class SceneManager {
    //单例模式改造
    static singleObj;
    static createObj(stage) {                               //stage是形参
        if (!SceneManager.singleObj) {
            SceneManager.singleObj = new SceneManager(stage); //stage是实参
        }
        return SceneManager.singleObj;
    }
    constructor(stage) {                                    //stage是形参
        this._stage = stage;
    }

    private _stage;
    private curScene;
    private _groupName;
    private _loading;
    private _nextScene;

    public loadScene(groupName, nextScene) {    //加载场景的方法
        this._groupName = groupName;
        this._nextScene = nextScene;
        this._loading = LoadingUI.createObj();
        this._stage.addChild(this._loading);    //加载进度条
        //添加资源组加载事件
        RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);  //资源组加载成功时触发
        RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);   //资源组加载失败时触发
        RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);      //资源组加载进度（每个条目加载成功时都会触发）
        RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);        //资源组里面单个条目加载失败时触发
        RES.loadGroup(groupName);//执行加载资源组
    }

    private onResourceLoadComplete(e) {
        if (e.groupName == this._groupName) {//这句话判断时必须的
            //移除监听事件，RES没有once事件
            RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
            RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
            let _nextScene = new this._nextScene(); //实例化要加载的场景
            this._stage.addChild(_nextScene);       //把要加载的场景添加到舞台
            if (this.curScene) {                    //如果curScene存在，即不是第一个进来场景
                if (_nextScene.enterAni) {          //如果加载进来的场景有自己的进入动画
                    _nextScene.enterAni(() => {
                        this._stage.removeChild(this.curScene);
                        this._stage.removeChild(this._loading);
                        this.curScene = _nextScene;
                    });
                } else {                            //如果加载进来的场景没有自己的进入动画，则执行默认动画
                    _nextScene.x = this._stage.stageWidth;
                    egret.Tween.get(_nextScene).to({ x: 0 }, 500).call(() => {
                        this._stage.removeChild(this.curScene);
                        this._stage.removeChild(this._loading);
                        this.curScene = _nextScene;
                    });
                }

            } else {                                //如果要加载进来的场景是第一个场景
                this._stage.removeChild(this._loading);
                this.curScene = _nextScene;         //加载进来的场景更新为当前场景
            }

        }
    }
    
    private onResourceLoadError(event: RES.ResourceEvent) {
        console.warn("Group:" + event.groupName + " has failed to load");
        this.onResourceLoadComplete(event);
    }
    private onResourceProgress(event: RES.ResourceEvent) {
        if (event.groupName == this._groupName) {
            this._loading.setProgress(event.itemsLoaded, event.itemsTotal);
        }
    }
    private onItemLoadError(event: RES.ResourceEvent) {
        console.warn("Url:" + event.resItem.url + " has failed to load");
    }
}


